<template>
  <div>
    <el-dialog :title="title" center  :visible="open" @close="close()" width="600px" :close-on-click-modal="false">
      <ht-form
        ref="editForm"
        defaultEventKey="configInfo"
        :rowNum="1"
        :open="open"
        :rowData="editForm"
        :dictMap="dictMap"
        :labelWidth="labelWidth"
        :formDiarules="rules"
        :formListOptions="formItemList"
        @isParentFn="isParentFn"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t("common.confirm") }}</el-button>
        <el-button @click="close">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getConfigInfo, addConfigInfo, checkConfigKey } from "@/api/authority_app/config/configInfo";
import { dialogMixin } from "@/libs/dialogMixin";
export default {
  name: "EditConfigInfo",
  mixins: [dialogMixin],
  data() {
    // 表单校验
    let checkConfigKeyUnique = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(`${this.$i18n.t("authority.configInfo.rules.configKeyEmpty")}`));
      } else {
        checkConfigKey({
          id: this.editForm.id,
          configKey: this.editForm.configKey,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(new Error(`${this.$i18n.t("authority.configInfo.rules.configKeyExist")}`));
          } else {
            callback();
          }
        });
      }
    };
    return {
      id: "",
      title: "",
      labelWidth: "128px",
      pageKey: "configInfo",
      rules: {
        configName: [
          {
            required: true,
            message: `${this.lang("authority.configInfo.rules.configNameEmpty")}`,
            trigger: "blur",
          },
          {
            min: 1,
            max: 100,
            message: `${this.lang("validate.lengthTip", { min: 1, max: 100 })}`,
            trigger: "blur",
          },
        ],
        configKey: [
          { required: true, trigger: "blur", validator: checkConfigKeyUnique },
          {
            min: 1,
            max: 100,
            message: `${this.lang("validate.lengthTip", { min: 1, max: 100 })}`,
            trigger: "blur",
          },
        ],
        configValue: [
          {
            required: true,
            message: `${this.lang("authority.configInfo.rules.configValueEmpty")}`,
            trigger: "blur",
          },
          {
            min: 1,
            max: 100,
            message: `${this.lang("validate.lengthTip", { min: 1, max: 100 })}`,
            trigger: "blur",
          },
        ],
        configType: [
          {
            required: true,
            message: `${this.lang("authority.configInfo.rules.configTypeEmpty")}`,
            trigger: "blur",
          },
        ],
        remark: [
          {
            max: 500,
            message: `${this.lang("validate.maxLeng", { max: 500 })}`,
            trigger: "blur",
          },
        ],
      },
      formItemList: [
        {
          type: "input",
          prop: "configName",
          defaultAttr: {
            label: `${this.lang("authority.configInfo.form.configName")}`,
            placeholder: `${this.lang("authority.configInfo.rules.configNamePLH")}`,
          },
        },
        {
          type: "input",
          prop: "configKey",
          defaultAttr: {
            label: `${this.lang("authority.configInfo.form.configKey")}`,
            placeholder: `${this.lang("authority.configInfo.rules.configKeyPLH")}`,
          },
        },
        {
          type: "input",
          prop: "configValue",
          defaultAttr: {
            label: `${this.lang("authority.configInfo.form.configValue")}`,
            placeholder: `${this.lang("authority.configInfo.rules.configValuePLH")}`,
          },
        },
        {
          type: "dict",
          dict: "sys_yes_no",
          prop: "configType",
          selectOptionList: [],
          defaultAttr: {
            disCleanable: true,
            label: `${this.lang("authority.configInfo.form.configType")}`,
            placeholder: `${this.lang("authority.configInfo.rules.configTypeEmpty")}`,
          },
        },
      ],
    };
  },
  mounted() {
    // 此处做数据字典数据加载初始化操作
    this.loadDicts(["sys_yes_no"]);
  },
  methods: {
    isParentFn(formData) {
      formData.id = this.id;
      if (formData.id) {
        delete formData.createTime;
        delete formData.updateTime;
        addConfigInfo(formData).then((res) => {
          this.msgSuccess(`${this.lang("common.editSuccess")}`);
          this.open = false;
          this.$emit("editComplete", true);
        });
      } else {
        delete formData.createTime;
        delete formData.updateTime;
        addConfigInfo(formData).then((res) => {
          this.msgSuccess(`${this.lang("common.addSuccess")}`);
          this.open = false;
          this.$emit("editComplete", true);
        });
      }
    },
    show(id = "") {
      this.id = id;
      this.$set(this, "editForm", {});
      if (id) {
        getConfigInfo(id)
          .then((res) => {
            this.$set(this, "editForm", res.result);
            this.open = true;
          })
          .catch((err) => {
            this.$message.error(`${this.lang("common.dataError")}`);
          });
      } else {
        this.$set(this.editForm, "id", id);
        this.open = true;
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.editForm.sonData();
    },
  },
};
</script>
